package com.iweb.day11;

import java.sql.*;

/**
 * @author WuYongheng
 * @date 2021/11/28
 * @description 执行带输入参数和普通输出参数的存储过程
 */
public class Test7 {
    public static void main(String[] args) {
        Connection conn = null;
        // 执行存储过程或函数
        CallableStatement statement = null;
        ResultSet resultSet = null;

        // 数据库连接信息
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
        String username = "hr";
        String password = "123456";
        try {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 连接对象
            conn = DriverManager.getConnection(url, username, password);
            if (conn != null) {
                System.out.println("连接成功");
            }
//            根据学号查询学生姓名和性别
            statement = conn.prepareCall("{call proc_queryStuInfo(?,?,?)}");
            statement.setInt(1, 1001);
            statement.registerOutParameter(2, Types.VARCHAR);
            statement.registerOutParameter(3, Types.VARCHAR);

            // 执行
            statement.execute();
            // 数据返回结果
            String studentName = statement.getString(2);
            int sex = statement.getInt(3);
            System.out.println("学生姓名：" + studentName);
            System.out.println("学生性别：" + sex);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
